/**
 * 文件名    ： RenameFileName.java
 * 版权       : XX科技有限公司。
 * 描述       : <描述>
 * 修改时间：2019年9月7日
 * 修改内容：<修改内容>
 */
package com.example.demo.application.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class RenameFileName {
	
	public static void main(String[] args) throws IOException {
		String PATH = "";
		PATH = "D:/home/novel_data/00/01";
		File file_tmp = new File(PATH);
		if (!file_tmp.exists()) {
			return;
		}
		else {
			PATH = file_tmp.getCanonicalPath() + "/";
		}
		String PATH2 = "D:/home/novel_data/00/03/";
	//	File file = new File(PATH  + "file_dir.txt");
		
		File fileDst = new File(PATH  + "file_dir_dst.txt");
		createFile(fileDst);
		
		FileInputStream fin = new FileInputStream(PATH  + "file_dir.txt");
		long lineNumber=1;
		File file3 = new File(PATH2  + "70-0.txt");
		createFile(file3);
		BufferedReader buffReader=new BufferedReader(new InputStreamReader(fin));
		String lineInfo = buffReader.readLine();
		List<String> lis = new ArrayList<String>();
		while(lineInfo!= null){
			System.out.println("第:"+lineNumber+"行数据是：" +lineInfo) ;
			if (lis.contains(lineInfo)) {
				System.out.println("第:"+lineNumber+"行数据是：" +lineInfo) ;
				lineInfo = buffReader.readLine();
				continue;
			}
			lis.add(lineInfo);
			if(lineInfo.length() <= 4) {
				lineInfo = buffReader.readLine();
				continue;
			}
			lineInfo = lineInfo.substring(0, lineInfo.length() -1) +".txt";
			System.out.println("第:"+lineNumber+"行数据是：" +lineInfo) ;
			lineNumber++;
			File file = new File(PATH  + lineInfo);
			if (!file.exists()) {
				lineInfo = buffReader.readLine();
				continue;
			}
			//新文件名
			File file2 = new File(PATH2  + lineInfo.substring(1, lineInfo.length()-5));
			createFile(file2);
			//写目录
			writeTxtFile(lineInfo.substring(1, lineInfo.length()-5), fileDst);
			//重命名
			writeTxtFile(readFile(file), file2);
			//合并
			writeTxtFile("\r\n\r\n\r\n" + lineInfo.substring(1, lineInfo.length()-5) +"\r\n\r\n"+ readFile(file), file3);
			
			lineInfo = buffReader.readLine();
		}
		
	}

	/**
	 * 按行读取文件
	 */
	private static String readFile(File file) {
//	        File file = new File(filename);
	        InputStream is = null;
	        Reader reader = null;
	        StringBuilder sb = new StringBuilder();
	        BufferedReader bufferedReader = null;
	        try {
	            is = new FileInputStream(file);
	            reader = new InputStreamReader(is);
	            bufferedReader = new BufferedReader(reader);
	            String line = null;
	            while ((line = bufferedReader.readLine()) != null) {
	            	line = line.replace("&nbsp;&nbsp;", " ");
	            	line = line.replace("&nbsp;", " ");
	            	line = line +"\r\n";
	            	sb.append(line);
	            }
	            return sb.toString();

	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (null != bufferedReader)
	                    bufferedReader.close();
	                if (null != reader)
	                    reader.close();
	                if (null != is)
	                    is.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
			return null;
	}

	public static void writeTxtFile(String content, File fileName) throws IOException {
		RandomAccessFile randomFile = null;
		try {
			// 打开一个随机访问文件流，按读写方式
			randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			String cont = content + "\r\n";
			randomFile.write(cont.getBytes("UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (randomFile != null) {
				randomFile.close();
			}
		}
	}
	
	public static void createFile(File fileName) throws IOException {
		try {
			if (!fileName.exists()) {
				fileName.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
